package com.dingding.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.junit.Test;

import com.dingding.common.core.util.ToolUtil;

import net.sf.json.JSONObject;

/**
 * @ClassName: HttpRequestUtil
 * @Description: http请求获取json数据 封装类
 * @author lijinchao
 * @date 2015-11-13 上午11:43:30
 * @version
 */
public class HttpRequestUtil {

	// 百度地图key
	private static final String MAP_Key = "Vqp95NEj4jBWshNCtzPXvYio";
	// 聚合数据key,仅用于手机号归属地城市查询
	private static final String JUHE_KEY = "234ac739c56c87ae5ceba61e7f2b3910";

	/**
	 * @Title: httpRequest
	 * @Description: 发送http请求
	 * @param urlString
	 * @author lijinchao
	 * @return String
	 * @throws
	 */
	private static String httpRequest(String urlString, String method,
			boolean isOutPut) {

		OutputStream outputStream = null;
		OutputStreamWriter outputStreamWriter = null;
		InputStream inputStream = null;
		InputStreamReader inputStreamReader = null;
		String jsonString = null;
		StringBuffer sb = new StringBuffer();
		BufferedReader buffer;

		try {
			URL url = new URL(urlString);
			URLConnection connection = url.openConnection();
			HttpURLConnection httpURLConnection = (HttpURLConnection) connection;

			if (isOutPut) {
				httpURLConnection.setDoOutput(true);
			} else {
				httpURLConnection.setDoOutput(false);
			}
			httpURLConnection.setRequestMethod(method);
			httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");

			if (isOutPut) {
				outputStream = httpURLConnection.getOutputStream();
				outputStreamWriter = new OutputStreamWriter(outputStream);

				outputStreamWriter.flush();
			}

			inputStream = httpURLConnection.getInputStream();
			inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			buffer = new BufferedReader(inputStreamReader);

			String line = null;
			while ((line = buffer.readLine()) != null) {
				sb.append(line);
			}
			if (isOutPut) {
				outputStreamWriter.close();
				outputStream.close();
			}
			buffer.close();
			inputStreamReader.close();
			inputStream.close();
			jsonString = sb.toString();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return jsonString;
	}

	/**
	 * @Title: getAttributionByJuhe
	 * @Description: 调用聚合数据查询手机归属地
	 * @param phone
	 * @author lijinchao
	 * @return String
	 * @throws
	 */
	public static String getAttributionByJuhe(String phoneNo) {

		JSONObject jsonObject = null;
		String url = "http://apis.juhe.cn/mobile/get?key=" + JUHE_KEY
				+ "&phone=" + phoneNo;
		String jsonString = httpRequest(url, "POST", true);

		// 获取jsonString的JSONObject对象
		jsonObject = JSONObject.fromObject(jsonString);

		if (jsonObject.getString("resultcode").equals("200")) {
			
			return jsonObject.getJSONObject("result").getString("city") + "市";
		}

		return "";
	}

	/**
	 * @Title: getDistanceByMap
	 * @Description: 通过百度地图api获取两点之间的距离
	 * @param @return
	 * @author lijinchao
	 * @return double
	 * @throws
	 */
	public static Double getDistanceByMap(double lng1, double lat1,
			double lng2, double lat2) {

		String lat1Str = String.valueOf(lat1);
		String lng1Str = String.valueOf(lng1);
		String lat2Str = String.valueOf(lat2);
		String lng2Str = String.valueOf(lng2);

		StringBuffer url = new StringBuffer(
				"http://api.map.baidu.com/direction/v1/routematrix?output=json&ak=");
		url.append(MAP_Key);
		url.append("&origins=" + lat1Str + "," + lng1Str);
		url.append("&destinations=" + lat2Str + "," + lng2Str);

		String jsonString = httpRequest(url.toString(), "GET", false);
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		Object object = jsonObject.getJSONObject("result")
				.getJSONArray("elements").getJSONObject(0)
				.getJSONObject("distance").get("value");

		Double dis = ToolUtil.castDouble(object)/ 1000;

		return dis;
	}

	/**
	 * 获取手机号归属地测试
	 */
	@Test
	public void getAttribution() {
		String city = getAttributionByJuhe("18354235662");

		System.out.println(city);
	}

	/**
	 * @Title: getDistance
	 * @Description: 计算两点距离测试
	 * @author lijinchao
	 * @return void
	 * @throws
	 */
	@Test
	public void getDistance() {

//		Integer distance = getDistanceByMap(116.517858, 39.802998, 116.556731,
//				39.802998);
//
//		System.out.println(String.valueOf(distance));
	}
}
